package com.tvunetworks.center.user.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.param.AddOrRemoveUserFeatureParam;
import com.tvunetworks.center.user.model.vo.UpdateUserFeatureVo;
import com.tvunetworks.center.user.model.vo.UserFeatureVo;
import com.tvunetworks.center.user.service.UserFeatureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: tvucc-aggregator
 * @description: UserFeatureController
 * @author lebronchen
 * @create: 2019-06-11 13:10
 **/
@RestController
@RequestMapping("/userFeature")
@Slf4j
public class UserFeatureController {

    @Autowired
    private UserFeatureService userFeatureService;

    /**
     *
     * @return Result
     */
    @GetMapping("/init")
    @Deprecated
    public Result<Object> initUserFeature(){
        userFeatureService.initUserFeature();
        return ResultUtil.success();
    }

    /**
     *
     * @param name name
     * @return Result
     */
    @GetMapping("/initAllHome")
    @Deprecated
    public Result<Object> initAllHome(@RequestParam String name){
        return userFeatureService.initAllHome(name);
    }

//    /**
//     * 列出用户拥有的feature
//     * @param userId
//     * @return
//     */
//    @GetMapping("/listOwn")
//    public Result<Object> listUserFeature(@RequestParam String userId){
//        Map<String, List<UserFeatureVo>> list = userFeatureService.listUserFeature(userId);
//        return ResultUtil.success(list);
//    }

    /**
     * 为用户添加、删除feature
     * @param param param
     * @param loginUser loginUser
     * @return Result
     */
    @PostMapping("/addOrRemove")
    public Result<Object> addOrRemoveFeature(@Logined LoginUser loginUser, @Valid AddOrRemoveUserFeatureParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return userFeatureService.addOrRemoveFeature(loginUser, param);
    }


    /**
     * 创建用户的时候 可选的feature列表
     * 用于创建用户时 可勾选列表
     * @param loginUser loginUser
     * @param targetUserRole targetUserRole
     * @return Result
     */
    @GetMapping("/listFeatureForCreate")
    public Result<UpdateUserFeatureVo> listFeatureForCreate(@Logined LoginUser loginUser, @RequestParam String targetUserRole){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return userFeatureService.listFeatureForCreate(loginUser, targetUserRole);
    }

    /**
     * 修改用户的时候 可选的feature列表
     * @param loginUser loginUser
     * @param modifyUserId modifyUserId
     * @return Result
     */
    @GetMapping("/listFeatureForModify")
    public Result<UpdateUserFeatureVo> listFeatureForModify(@Logined LoginUser loginUser, @RequestParam String modifyUserId){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return userFeatureService.listFeatureForModify(loginUser, modifyUserId);
    }

    /**
     * 根据userid查询用户拥有的权限
     * @param userId userId
     * @return 权限集合
     */
    @GetMapping("/listFeatureByUserId")
    public List<UserFeatureVo> listFeatureByUserId(@RequestParam String userId){
        return userFeatureService.listFeatureByUserId(userId);
    }
}
